# coding:utf-8
import datetime
import os
import sys

from app.libs.error_code import RunScriptError
from app.libs.make_deal import make_deal_file


def run_py(response, condition, var_dick):
    import importlib.util
    module_path = make_deal_file(response, condition, var_dick)
    module_name = "my_module"
    try:
        spec = importlib.util.spec_from_file_location(module_name, module_path)
        my_module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(my_module)
    except Exception:
        # raise RunScriptError(msg=err())
        return err()
    else:
        return my_module.store
    finally:
        # pass
        os.remove(module_path)


def err():
    import traceback
    error_info = ''
    exc_type, exc_value, exc_traceback = sys.exc_info()
    for filename, linenum, funcname, source in traceback.extract_tb(exc_traceback):
        err = 'File %s, \nin %s, line %s, %s' % (filename, funcname, str(linenum), source)
        time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        error_info = '错误类型:%s\n错误信息: %s\n错误值: %s\n时间: %s' % (
            str(exc_type), err, exc_value, time)
    return error_info


# asyncio.run(run_case(testcases, before_request=before_request, after_request=after_request))

if __name__ == '__main__':
    pass
